
<template>
  <el-dialog :close-on-click-modal="false" :title="$tt('label_tms_waybill_volume_split')" :visible.sync="visible"
    custom-class="dialog_default">

    <el-form :label-width="$globalCss.formLabelWidth_default" v-loading="loading" :model="form" :rules="rules" ref="Form">

      <div class="flex_row_wrap">
        <el-form-item :label="$tt('label_tms_waybill_waybill_number')">
          {{ item.waybillCode }}
        </el-form-item>
        <el-form-item :label="$tt('label_tms_waybill_waybill_type')">
          {{ $enumShow(item.transportSection, "TransportSectionEnum") }}
        </el-form-item>
        <el-form-item :label="$tt('label_tms_waybill_transport_mode')">
          {{ $enumShow(item.transportModel, "LogisticsProductsTypeEnum") }}
        </el-form-item>
      </div>
      <div class="flex_row_wrap">

        <el-form-item :label="$tt('label_tms_waybill_total_cartons')">
          {{ item.totalBox }}
        </el-form-item>
        <el-form-item :label="$tt('label_total_sku_count')">
          {{ item.totalQuantity }}
        </el-form-item>
        <el-form-item :label="$tt('label_tms_waybill_total_weight')">
          {{ item.totalWeight }}
        </el-form-item>
        <el-form-item :label="$tt('label_total_volume')">
          {{ item.totalVolume }}
        </el-form-item>
      </div>
      <div class="flex_row_wrap">

        <el-form-item :label="$tt('label_tms_residue_box')">
          {{ item.lastBox }}
        </el-form-item>
        <el-form-item :label="$tt('label_tms_residue_piece')">
          {{ item.lastQuantity }}
        </el-form-item>
        <el-form-item :label="$tt('label_tms_residue_weight')">
          {{ item.lastWeight }}
        </el-form-item>
        <el-form-item :label="$tt('label_tms_residue_volume')">
          {{ item.lastVolume }}
        </el-form-item>
      </div>
      <div class="flex_row_wrap">


        <el-form-item :label="$tt('label_tms_split_box')" prop="totalBox">
          <el-input v-model="form.totalBox"></el-input>
        </el-form-item>
        <el-form-item :label="$tt('label_tms_split_piece')" prop="totalQuantity">
          <el-input v-model="form.totalQuantity"></el-input>
        </el-form-item>
        <el-form-item :label="$tt('label_tms_split_weight')" prop="totalWeight">
          <el-input v-model="form.totalWeight"></el-input>
        </el-form-item>
        <el-form-item :label="$tt('label_tms_split_volume')" prop="totalVolume">
          <el-input v-model="form.totalVolume"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button @click="clickBreak">{{$tt('label_add')}}</el-button>
        </el-form-item>
      </div>
      <ex_super_table :data="itemList" v-bind="option_table" :max-height="400" />

    </el-form>
    <span slot="footer" class="dialog_footer">
      <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
      <el-button type="primary" @click="submitBefore">{{ $tt('label_submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {

  data() {
    return {
      visible: false,

      loading: false,
      item: {},
      form: {},
      itemList: [],

      rules: {
        totalQuantity: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.PureNumber(v)) {
              cb(this.$tt('tip_only_positive_pure_decimal'))
            } else if (Number(v) > this.item.lastQuantity) {
              cb(this.$tt('label_tms_not_gt_residue_piece'))
            } else {
              cb()
            }
          }, trigger: 'blur'
        },
        totalBox: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.PureNumber(v)) {
              cb(this.$tt('tip_only_positive_pure_decimal'))
            } else if (Number(v) > Number(this.item.lastBox)) {
              cb(this.$tt('label_tms_not_gt_residue_box'))
            } else {
              cb()
            }
          }, trigger: 'blur'
        },
        totalWeight: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v, 3, 9)) {
              cb(this.$tt("tip_check_decimal_digit_int", { digit: 3, int: 9 }))
            } else if (Number(v) > this.item.lastWeight) {
              cb(this.$tt('label_tms_not_gt_residue_weight'))
            } else {
              cb()
            }
          }, trigger: 'blur'
        },
        totalVolume: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v, 2, 9)) {
              cb(this.$tt("tip_check_decimal_digit_int", { digit: 2, int: 9 }))
            } else if (Number(v) > Number(this.item.lastVolume)) {
              cb(this.$tt('label_tms_not_gt_residue_volume'))
            } else {
              cb()
            }
          }, trigger: 'blur'
        },
      },
      option_table: {
        model: [
          { key: 'totalQuantity', label: 'label_tms_track_pkgs', 'min-width': 100, 'show-overflow-tooltip': true, },
          { key: 'totalBox', label: 'label_tms_track_pcs', 'min-width': 100, 'show-overflow-tooltip': true, },
          { key: 'totalWeight', label: 'label_tms_track_weight', 'min-width': 100, 'show-overflow-tooltip': true, },
          { key: 'totalVolume', label: 'label_tms_track_volume', 'min-width': 100, 'show-overflow-tooltip': true, },
          {
            type: 'operate', operate: [
              {
                label: 'label_remove', class: 'button_text_danger', click: (row, i, list) => {
                  list.splice(i, 1)
                  this.refreshLast()
                }
              }
            ]
          }
        ]
      }

    }
  },
  props: {


  },
  methods: {


    show(item,) {
      this.visible = true
      this.itemList = []
      this.item = Object.assign({ lastBox: 0, lastQuantity: 0, lastVolume: 0, lastWeight: 0, }, _.cloneDeep(item))
      this.form = {}
      this.refreshLast()

    },
    refreshLast() {

      this.item.lastQuantity = this.$math_sub(this.item.totalQuantity, _.reduce(this.itemList, (sum, it) => this.$math_add(sum, it.totalQuantity), 0))
      this.item.lastBox = this.$math_sub(this.item.totalBox, _.reduce(this.itemList, (sum, it) => this.$math_add(sum, it.totalBox), 0))
      this.item.lastWeight = this.$math_sub(this.item.totalWeight, _.reduce(this.itemList, (sum, it) => this.$math_add(sum, it.totalWeight), 0))
      this.item.lastVolume = this.$math_sub(this.item.totalVolume, _.reduce(this.itemList, (sum, it) => this.$math_add(sum, it.totalVolume), 0))


    },
    clickBreak() {
      this.$refs['Form'].validate((valid) => {
        if (valid) {
          let form = _.cloneDeep(this.form)
          _.forOwn(form, (v, k) => form[k] = Number(v))
          this.itemList.push(form)
          this.refreshLast()
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    },
    hide() {
      this.visible = false
    },
    submit(last) {
      this.$ajax({
        url: "/tms-api/waybill/TmsWaybillInfoOperationRpc/breakUpByQty.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          version: this.item.version,
          id: this.item.id,
          qtyDTOList: last ? [...this.itemList, last] : this.itemList
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.hide()
          this.$emit('submitSuccess')
        },
      })
    },
    submitBefore() {
      if (_.isEmpty(this.itemList)) {
        return this.$message.error(this.$tt('label_tms_not_split_product'))
      }
      if ((this.item.lastVolume > 0 || this.item.lastBox > 0 || this.item.lastQuantity > 0 || this.item.lastWeight > 0) &&
        (this.item.lastVolume <= 0 || this.item.lastBox <= 0 || this.item.lastQuantity <= 0 || this.item.lastWeight <= 0)) {
        return this.$message.error(this.$tt('label_tms_residue_product_def_gen_order'))
      }
      let last = null
      if (this.item.lastVolume > 0 || this.item.lastBox > 0 || this.item.lastQuantity > 0 || this.item.lastWeight > 0) {
        last = {
          totalBox: this.item.lastBox,
          totalQuantity: this.item.lastQuantity,
          totalWeight: this.item.lastWeight,
          totalVolume: this.item.lastVolume,
        }
      }


      if (this.item.lastBox > 0 || this.item.lastQuantity > 0) {
        this.$confirm(this.$tt('label_tms_lastBox_hint', { box: this.item.lastBox, qty: this.item.lastQuantity }), this.$tt('label_tip'), {
          type: 'warning'
        }).then(() => {
          this.submit(last)
        }).catch(() => {
        });
      } else {
        this.submit(last)
      }





    },

  }
}
</script>
